<?php
/*
 * @package Joomla 1.5
 * @copyright Copyright (C) 2005 Open Source Matters. All rights reserved.
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
 *
 * @component jZmluvy
 * @copyright Copyright (C) Jan Pavelka www.phoca.cz
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
 * @ http://docs.joomla.org/Manifest_files
 */
defined('_JEXEC') or die();

 require_once( JPATH_COMPONENT.'/helper.php' );


class jZmluvyControllerInstall extends JController
{
	function __construct() {
		parent::__construct();
		$this->registerTask( 'install'  , 'install' );
		$this->registerTask( 'upgrade'  , 'upgrade' );
     
    $this->verze = jZmluvyHelper::getVersion();		
    
  }

	function install() {		
		$db			= &JFactory::getDBO();
		$msgSQL 	= $msgError = '';		
		
		// ------------------------------------------
		//  jZmluvy
		// ------------------------------------------
		$query ='DROP TABLE IF EXISTS '.$db->nameQuote('#__jzmluvy').';';
		$db->setQuery( $query );
		if (!$result = $db->query()){$msgSQL .= $db->stderr() . '<br />';}
		
  $query ="CREATE TABLE IF NOT EXISTS `#__jzmluvy` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `title` text NOT NULL,
  `alias` varchar(255) NOT NULL default '',
  `fulltext` mediumtext NOT NULL,
  `jedcislo` varchar(255) NOT NULL,
  `state` tinyint(3) NOT NULL default '0',
  `catid` int(11) unsigned NOT NULL default '0',
  `created` datetime NOT NULL default '0000-00-00 00:00:00',
  `created_by` int(11) unsigned NOT NULL default '0',
  `created_by_alias` varchar(255) NOT NULL default '',
  `modified` datetime NOT NULL default '0000-00-00 00:00:00',
  `modified_by` int(11) unsigned NOT NULL default '0',
  `checked_out` int(11) unsigned NOT NULL default '0',
  `checked_out_time` datetime NOT NULL default '0000-00-00 00:00:00',
  `publish_up` date NOT NULL default '0000-00-00',
  `publish_down` date NOT NULL default '0000-00-00',
  `file` varchar(255) NOT NULL,
  `filename` varchar(255) NOT NULL,
  `extension` varchar(10) NOT NULL,
  `ordering` int(11) NOT NULL default '0',
  `hits` int(11) NOT NULL,
  `vlastni1` varchar(255) NOT NULL,
  `vlastni2` varchar(255) NOT NULL,
  `faktura` varchar(50) NOT NULL default '',
  `intCislo` varchar(50) NOT NULL default '',
  `dodavatel` int(11) unsigned NOT NULL default '0',
  `suma` double NOT NULL,
  `hits_detail` int(11) NOT NULL,
  `datUhrady` date NOT NULL default '0000-00-00',
  `datZver` date NOT NULL default '0000-00-00',
  `sposob` varchar(255) NOT NULL default '',
  `poznamka` text NOT NULL,
  `file2` varchar(255) NOT NULL,
  `filename2` varchar(255) NOT NULL,
  `hits2` int(11) NOT NULL,
  `extension2` varchar(10) NOT NULL,
  `typ` varchar(112) NOT NULL COMMENT 'vystavena alebo prijata 0,1',
  `mena` varchar(10) NOT NULL default '',
  `sposob_uhrady` int(10) NOT NULL,
  `datum_podpisu` date NOT NULL default '0000-00-00',
  PRIMARY KEY  (`id`),
  KEY `idx_checkout` (`checked_out`),
  KEY `idx_state` (`state`),
  KEY `idx_catid` (`catid`),
  KEY `idx_createdby` (`created_by`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;";


		$db->setQuery( $query );
		if (!$result = $db->query()){$msgSQL .= $db->stderr() . '<br />';}
		
		// ------------------------------------------
		// jZmluvy - kategorie
		// ------------------------------------------
		$query=' DROP TABLE IF EXISTS '.$db->nameQuote('#__jzmluvy_kategorie').';'."\n";
		$db->setQuery( $query );
		if (!$result = $db->query()){$msgSQL .= $db->stderr() . '<br />';}
		
		$query="CREATE TABLE IF NOT EXISTS `#__jzmluvy_kategorie` (
  `id` int(11) NOT NULL auto_increment,
  `parent_id` int(11) NOT NULL default '0',
  `title` varchar(255) NOT NULL default '',
  `name` varchar(255) NOT NULL default '',
  `alias` varchar(255) NOT NULL default '',
  `image` varchar(255) NOT NULL default '',
  `section` varchar(50) NOT NULL default '',
  `image_position` varchar(30) NOT NULL default '',
  `description` text NOT NULL,
  `published` tinyint(1) NOT NULL default '0',
  `checked_out` int(11) unsigned NOT NULL default '0',
  `checked_out_time` datetime NOT NULL default '0000-00-00 00:00:00',
  `editor` varchar(50) default NULL,
  `ordering` int(11) NOT NULL default '0',
  `access` tinyint(3) unsigned NOT NULL default '0',
  `count` int(11) NOT NULL default '0',
  `params` text NOT NULL,
  PRIMARY KEY  (`id`),
  KEY `cat_idx` (`section`,`published`,`access`),
  KEY `idx_access` (`access`),
  KEY `idx_checkout` (`checked_out`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=4;";
		
		$db->setQuery( $query );
		if (!$result = $db->query()){$msgSQL .= $db->stderr() . '<br />';}
    
    $query="INSERT INTO `#__jzmluvy_kategorie` (`id`, `parent_id`, `title`, `name`, `alias`, `image`, `section`, `image_position`, `description`, `published`, `checked_out`, `checked_out_time`, `editor`, `ordering`, `access`, `count`, `params`) VALUES
    (1, 0, 'Faktúra', '', 'faktura', '', '', '', '', 1, 0, '0000-00-00 00:00:00', NULL, 0, 0, 0, ''),
    (2, 0, 'Zmluva', '', 'zmluva', '', '', '', '', 1, 0, '0000-00-00 00:00:00', NULL, 0, 0, 0, ''),
    (3, 0, 'Objednávka', '', 'objednavka', '', '', '', '', 1, 0, '0000-00-00 00:00:00', NULL, 0, 0, 0, '');";    
		
		$db->setQuery( $query );
		if (!$result = $db->query()){$msgSQL .= $db->stderr() . '<br />';}
    
		// ------------------------------------------
		// jZmluvy - nastavenie
		// ------------------------------------------
		$query=' DROP TABLE IF EXISTS '.$db->nameQuote('#__jzmluvy_nastaveni').';'."\n";
		
		$db->setQuery( $query );
		if (!$result = $db->query()){$msgSQL .= $db->stderr() . '<br />';}
		
		$query="CREATE TABLE IF NOT EXISTS `#__jzmluvy_nastaveni` (
  `id` int(11) NOT NULL auto_increment,
  `intro` text NOT NULL,
  `kontakty` tinyint(4) NOT NULL,
  `kontakty_link` tinyint(4) NOT NULL,
  `podpis` varchar(100) NOT NULL,
  `podpis_funkcia` varchar(100) NOT NULL,
  `stat` varchar(100) NOT NULL,
  `mena` varchar(100) NOT NULL,
  `dph` tinyint(4) NOT NULL,
  `subory` tinyint(4) NOT NULL,
  `subory_rezerva` tinyint(4) NOT NULL,
  `datum_text` varchar(100) NOT NULL,
  `datum_format` varchar(100) NOT NULL,
  `crz` tinyint(4) NOT NULL,
  `crz_link` tinyint(100) NOT NULL,
  `crz_web` varchar(255) NOT NULL,
  `obj_dat_plnenia` tinyint(4) NOT NULL,
  `fak_int_cislo` tinyint(4) NOT NULL,
  `fak_dat_platnosti` tinyint(4) NOT NULL,
  `fak_dat_uhrady` tinyint(4) NOT NULL,
  `zml_dat_podpisu` tinyint(4) NOT NULL,
  `zml_dat_platnosti` tinyint(4) NOT NULL,
  `zml_dat_ucinnosti` tinyint(4) NOT NULL,
  `partner_spis_znacka` tinyint(4) NOT NULL,
  `partner_logo` tinyint(4) NOT NULL,
  `partner_dic` tinyint(4) NOT NULL,
  `partner_pocet_obj` int(11) NOT NULL,
  `debug` tinyint(4) NOT NULL,
  `ver` varchar(10) NOT NULL,
  PRIMARY KEY  (`id`),
  KEY `id` (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=2;";

		
		$db->setQuery( $query );
		if (!$result = $db->query()){$msgSQL .= $db->stderr() . '<br />';}
		
    		$query="INSERT INTO `jos_jzmluvy_nastaveni` (`id`, `intro`, `kontakty`, `kontakty_link`, `podpis`, `podpis_funkcia`, `stat`, `mena`, `dph`, `subory`, `subory_rezerva`, `datum_text`, `datum_format`, `crz`, `crz_link`, `crz_web`, `obj_dat_plnenia`, `fak_int_cislo`, `fak_dat_platnosti`, `fak_dat_uhrady`, `zml_dat_podpisu`, `zml_dat_platnosti`, `zml_dat_ucinnosti`, `partner_spis_znacka`, `partner_logo`, `partner_dic`, `partner_pocet_obj`, `debug`, `ver`) VALUES
        (1, '', 1, 1, '', '', 'Slovensko', '€', 1, 1, 1, 'nezadané', 'j.n.Y', 1, 1, '', 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, '0.2.0');";

		$db->setQuery( $query );
		if (!$result = $db->query()){$msgSQL .= $db->stderr() . '<br />';}



		// ------------------------------------------
		// jzmluvy - partner
		// ------------------------------------------

		$query=' DROP TABLE IF EXISTS '.$db->nameQuote('#__jzmluvy_partner').';'."\n";
		
		$db->setQuery( $query );
		if (!$result = $db->query()){$msgSQL .= $db->stderr() . '<br />';}
		
		$query="CREATE TABLE IF NOT EXISTS `#__jzmluvy_partner` (
  `id` int(11) NOT NULL auto_increment,
  `parent_id` int(11) NOT NULL default '0',
  `title` varchar(255) NOT NULL default '',
  `meno` varchar(255) NOT NULL default '',
  `ulica` varchar(255) NOT NULL default '',
  `mesto` varchar(255) NOT NULL default '',
  `psc` varchar(255) NOT NULL default '',
  `stat` varchar(255) NOT NULL default '',
  `ico` varchar(20) NOT NULL default '',
  `dic` varchar(20) NOT NULL default '',
  `ic_dph` tinyint(4) NOT NULL,
  `alias` varchar(255) NOT NULL default '',
  `description` text NOT NULL,
  `published` tinyint(1) NOT NULL default '0',
  `typ` tinyint(1) NOT NULL default '1',
  `reserverd` varchar(255) NOT NULL default '',
  `checked_out` int(11) unsigned NOT NULL default '0',
  `checked_out_time` datetime NOT NULL default '0000-00-00 00:00:00',
  `editor` varchar(50) default NULL,
  `ordering` int(11) NOT NULL default '0',
  `access` tinyint(3) unsigned NOT NULL default '0',
  `count` int(11) NOT NULL default '0',
  `params` text NOT NULL,
  `or_sud` int(11) NOT NULL default '0',
  `or_odd` int(11) NOT NULL default '0',
  `or_vlozka` int(11) default NULL,
  `logo` varchar(100) NOT NULL,
  PRIMARY KEY  (`id`),
  KEY `cat_idx` (`published`,`access`),
  KEY `idx_access` (`access`),
  KEY `idx_checkout` (`checked_out`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;";

    $db->setQuery( $query );
		if (!$result = $db->query()){$msgSQL .= $db->stderr() . '<br />';}
		
    		
		// Error
		if ($msgSQL !='') {
			$msgError .= '<br />' . $msgSQL;
		}
		
		// End Message
		if ($msgError !='') {
			$msg = JText::_( 'INSTALL-FAIL' ) . ': ' . $msgError;
		} else {
			$msg = JText::_( 'INSTALL-SUCCESS' ) .' '.$this->verze['version'];
		}
		
		$link = 'index.php?option=com_jzmluvy';
		$this->setRedirect($link, $msg);
	}
	
	
	
	
	function upgrade() {
		
    $db			=& JFactory::getDBO();
		$msgSQL 	= $msgError = '';
		
		// UPGRADE jZmluvy 0.2.0 VERSION
		// ------------------------------------------
		// jZmluvy - nastavenie
		// ------------------------------------------

	  /*
    $query="ALTER TABLE  `#__jzmluvy_nastaveni` ADD  `fak_int_cislo` TINYINT NOT NULL AFTER  `obj_dat_plnenia`;";
		$db->setQuery( $query );
		if (!$result = $db->query()){$msgSQL .= $db->stderr() . '<br />';}
		
    $query="ALTER TABLE  `#__jzmluvy_nastaveni` ADD  `ver` VARCHAR( 10 ) NOT NULL AFTER  `debug`;";
		$db->setQuery( $query );
		if (!$result = $db->query()){$msgSQL .= $db->stderr() . '<br />';}
    */

    $query=' DROP TABLE IF EXISTS '.$db->nameQuote('#__jzmluvy_nastaveni').';'."\n";
		
		$db->setQuery( $query );
		if (!$result = $db->query()){$msgSQL .= $db->stderr() . '<br />';}
		
		$query="CREATE TABLE IF NOT EXISTS `#__jzmluvy_nastaveni` (
  `id` int(11) NOT NULL auto_increment,
  `intro` text NOT NULL,
  `kontakty` tinyint(4) NOT NULL,
  `kontakty_link` tinyint(4) NOT NULL,
  `podpis` varchar(100) NOT NULL,
  `podpis_funkcia` varchar(100) NOT NULL,
  `stat` varchar(100) NOT NULL,
  `mena` varchar(100) NOT NULL,
  `dph` tinyint(4) NOT NULL,
  `subory` tinyint(4) NOT NULL,
  `subory_rezerva` tinyint(4) NOT NULL,
  `datum_text` varchar(100) NOT NULL,
  `datum_format` varchar(100) NOT NULL,
  `crz` tinyint(4) NOT NULL,
  `crz_link` tinyint(100) NOT NULL,
  `crz_web` varchar(255) NOT NULL,
  `obj_dat_plnenia` tinyint(4) NOT NULL,
  `fak_int_cislo` tinyint(4) NOT NULL,
  `fak_dat_platnosti` tinyint(4) NOT NULL,
  `fak_dat_uhrady` tinyint(4) NOT NULL,
  `zml_dat_podpisu` tinyint(4) NOT NULL,
  `zml_dat_platnosti` tinyint(4) NOT NULL,
  `zml_dat_ucinnosti` tinyint(4) NOT NULL,
  `partner_spis_znacka` tinyint(4) NOT NULL,
  `partner_logo` tinyint(4) NOT NULL,
  `partner_dic` tinyint(4) NOT NULL,
  `partner_pocet_obj` int(11) NOT NULL,
  `debug` tinyint(4) NOT NULL,
  `ver` varchar(10) NOT NULL,
  PRIMARY KEY  (`id`),
  KEY `id` (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=2;";

		
		$db->setQuery( $query );
		if (!$result = $db->query()){$msgSQL .= $db->stderr() . '<br />';}
		
    		$query="INSERT INTO `jos_jzmluvy_nastaveni` (`id`, `intro`, `kontakty`, `kontakty_link`, `podpis`, `podpis_funkcia`, `stat`, `mena`, `dph`, `subory`, `subory_rezerva`, `datum_text`, `datum_format`, `crz`, `crz_link`, `crz_web`, `obj_dat_plnenia`, `fak_int_cislo`, `fak_dat_platnosti`, `fak_dat_uhrady`, `zml_dat_podpisu`, `zml_dat_platnosti`, `zml_dat_ucinnosti`, `partner_spis_znacka`, `partner_logo`, `partner_dic`, `partner_pocet_obj`, `debug`, `ver`) VALUES
        (1, '', 1, 1, '', '', 'Slovensko', '€', 1, 1, 1, 'nezadané', 'j.n.Y', 1, 1, '', 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, '0.2.0');";

		$db->setQuery( $query );
		if (!$result = $db->query()){$msgSQL .= $db->stderr() . '<br />';}
    
    
    
		// CHECK TABLES
		$query =' SELECT * FROM '.$db->nameQuote('#__jzmluvy').' LIMIT 1;';
		$db->setQuery( $query );
		$result = $db->loadResult();
		if ($db->getErrorNum()) {
			$msgSQL .= $db->getErrorMsg(). '<br />';
		}
		
		$query=' SELECT * FROM '.$db->nameQuote('#__jzmluvy_kategorie').' LIMIT 1;'."\n";
		$db->setQuery( $query );
		$result = $db->loadResult();
		if ($db->getErrorNum()) {
			$msgSQL .= $db->getErrorMsg(). '<br />';
		}
		
		$query=' SELECT * FROM '.$db->nameQuote('#__jzmluvy_nastaveni').' LIMIT 1;'."\n";
		$db->setQuery( $query );
		$result = $db->loadResult();
		if ($db->getErrorNum()) {
			$msgSQL .= $db->getErrorMsg(). '<br />';
		}
		
		$query=' SELECT * FROM '.$db->nameQuote('#__jzmluvy_partner').' LIMIT 1;'."\n";
		$db->setQuery( $query );
		$result = $db->loadResult();
		if ($db->getErrorNum()) {
			$msgSQL .= $db->getErrorMsg(). '<br />';
		}

    		
		// Error
		if ($msgSQL !='') {
			$msgError .= '<br />' . $msgSQL;
		}

		// End Message
		if ($msgError !='') {
			$msg = JText::_( 'UPGRADE-FAIL' ) . ': ' . $msgError;
		} else {
			$msg = JText::_( 'UPGRADE-SUCCESS' ).' '.$this->verze['version'];
		}
		
		$link = 'index.php?option=com_jzmluvy';

		$this->setRedirect($link, $msg);
	}
	
	
	function AddColumnIfNotExists(&$errorMsg, $table, $column, $attributes = "INT( 11 ) NOT NULL DEFAULT '0'", $after = '' ) {
		
		global $mainframe;
		$db				=& JFactory::getDBO();
		$columnExists 	= false;

		$query = 'SHOW COLUMNS FROM '.$table;
		$db->setQuery( $query );
		if (!$result = $db->query()){return false;}
		$columnData = $db->loadObjectList();
		
		foreach ($columnData as $valueColumn) {
			if ($valueColumn->Field == $column) {
				$columnExists = true;
				break;
			}
		}
		
		if (!$columnExists) {
			if ($after != '') {
				$query = 'ALTER TABLE '.$db->nameQuote($table).' ADD '.$db->nameQuote($column).' '.$attributes.' AFTER '.$db->nameQuote($after).';';
			} else {
				$query = 'ALTER TABLE '.$db->nameQuote($table).' ADD '.$db->nameQuote($column).' '.$attributes.';';
			}
			$db->setQuery( $query );
			if (!$result = $db->query()){return false;}
			$errorMsg = 'notexistcreated';
		}
		
		return true;
	}
}
?>
